import axios from '../config/axios-config.js'

// Get the ICE server configuration
export const getIceServers = async () => { // /api/getIceServers
  try {
    const response = await axios.get('/api/getIceServers')
    return response.data
  } catch (error) {
    console.error('Error fetching ICE servers:', error)
    throw new Error('Failed to fetch ICE servers. Please try again later.')
  }
}

// Send offer and get answer
export const handleCall = async (url, offer) => { // /api/call
  try {
    const response = await axios.post(url, offer)
    return response.data
  } catch (error) {
    console.error(`Error handling call:`, error)
    throw new Error('Failed to handle call. Please try again later.')
  }
}

// Create an offer
export const createOffer = async (url) => { // /api/createOffer
  try {
    const response = await axios.get(url)
    return response.data
  } catch (error) {
    console.error(`Error creating offer:`, error)
    throw new Error('Failed to create offer. Please try again later.')
  }
}

// Set an answer
export const setAnswer = async (url, answer) => { // /api/setAnswer
  try {
    const response = await axios.post(url, answer)
    return response.data
  } catch (error) {
    console.error(`Error setting answer:`, error)
    throw new Error('Failed to set answer. Please try again later.')
  }
}

// Close the call
export const handleCloseCall = async (url) => { // /api/hangup
  try {
    const response = await axios.get(url)
    return response.data
  } catch (error) {
    console.error(`Error closing call:`, error)
    throw new Error('Failed to close call. Please try again later.')
  }
}

// Add ICE candidates
export const addIceCandidate = async (url, candidate) => { // /api/addIceCandidate
  try {
    const response = await axios.post(url, candidate)
    return response.data
  } catch (error) {
    console.error('Error adding ICE candidate:', error)
    throw new Error('Failed to add ICE candidate. Please try again later.')
  }
}

// Get a list of ICE candidates
export const getIceCandidate = async (url) => { // /api/getIceCandidate
  try {
    const response = await axios.get(url)
    return response.data
  } catch (error) {
    console.error(`Error getting ICE candidates:`, error)
    throw new Error('Failed to get ICE candidates. Please try again later.')
  }
}
